<?php

class album {

	const LOCATION_ALL = 1;
	const LOCATION_GROUP = 2;
	const LOCATION_USER = 3;
	
	public static function getAlbum($isID){
		$dbCon = db::singleton();
		
		$sql = "SELECT * FROM " . SYS_DB_DBNAME . ".imageset WHERE isID=" . $dbCon->real_escape_string((int)$isID);
		
		return $dbCon->oneRow($sql);
	}
	
	public static function getOwner(user &$userObj, $isID){
		$dbCon = db::singleton();
		
		$sql = "SELECT user_uID, group_gID FROM " . SYS_DB_DBNAME . ".imageset WHERE isID=" . $dbCon->real_escape_string($isID);
		
		return $dbCon->oneRow($sql);
	}
	
	public static function getUserAlbums(user &$userObj){
		$dbCon = db::singleton();
		
		$sql = "SELECT * FROM " . SYS_DB_DBNAME . ".imageset WHERE user_uID=" . $userObj->getuID();
		
		return $dbCon->single($sql);
	}

	public static function nameExists(user &$userObj, $name, $library = album::LOCATION_USER, $alid = 0, $ignoreMe = false){
		$dbCon = db::singleton();
		
		switch($library){
		
			case album::LOCATION_USER:
				$sql = "SELECT COUNT(*) FROM " . SYS_DB_DBNAME . ".imageset WHERE user_uID = " . $dbCon->real_escape_string($userObj->getuID()) . " AND name LIKE '" . $dbCon->real_escape_string($name) . "'";
				$res = $dbCon->oneRow($sql);
				
				if($ignoreMe) $res['COUNT(*)']--;
				
				return $res['COUNT(*)'];
			break;
			
			case album::LOCATION_GROUP:
				$sql = "SELECT COUNT(*) FROM " . SYS_DB_DBNAME . ".imageset WHERE group_gID = " . $dbCon->real_escape_string($alid) . " AND name LIKE '" . $dbCon->real_escape_string($name) . "'";
				$res = $dbCon->oneRow($sql);
				
				if($ignoreMe) $res['COUNT(*)']--;
				
				return $res['COUNT(*)'];
			break;
		
		
		}
		
		throw new PicBoardException("Invalid Library provided to album::nameExists");
	}

	
	public static function updateDetails(user &$userObj, $isID, $name, $description){
		$dbCon = db::singleton();
		
		// Do we have access to update this album?
		// Get the owner of the album.
		$ownerDetails = album::getOwner($userObj, $isID);
		
		$auth = false;
		
		if( $ownerDetails['user_uID'] == $userObj->getuID() ) $auth = true;
		
		if( $ownerDetails['group_gID'] != "" ){
			// User needs to have admin clearance to change the album name
			if( $userObj->isInGroup($ownerDetails['group_gID'], user::GROUP_ADMINONLY) ){
				$auth = true;
			} else {
				$auth = false;
			}
		}
		
		if($auth){
			
			// Is the name different?
			$album = album::getAlbum($isID);
			
			if($name != $album['name']){
				
				// Is the name unique?
				$lib = ($ownerDetails['group_gID'] != "") ? album::LOCATION_GROUP : album::LOCATION_USER;
				$id = $ownerDetails['group_gID'];
				
				if(album::nameExists($userObj, $name, $lib, $id)) throw new PicBoardException("You cannot use this name as it is already in use");
			}

			$sql = "UPDATE " . SYS_DB_DBNAME . ".imageset SET name='" . $dbCon->real_escape_string($name) . "', description='" . $dbCon->real_escape_string($description) . "' WHERE isID=" . $dbCon->real_escape_string($isID);
			
			$dbCon->single($sql);
			
			return true;
			
		} else {
			return false;
		}
	}
	
	public static function deleteAlbum(user &$userObj, $isID){
		if($userObj->getAccessLevel() < 0) throw new PicBoardException("Unauthorised");
		
		/*
		SELECT group_gID, user_uID
		FROM imageset
		WHERE isID=4
		*/

		
		// Disallow if this is the 'My Album' imageset.
		
		// Get the name of the album
		$album = album::getAlbum($isID);
		
		if($album['name'] == "My Album") throw new PicBoardException("You cannot delete the default album.");

		$dbCon = db::singleton();

		$dbCon->startTransaction();

		try {

			$sql = "SELECT group_gID, user_uID FROM " . SYS_DB_DBNAME . ".imageset WHERE isID=" . $dbCon->real_escape_string((int)$isID);
			
			$res = $dbCon->oneRow($sql);
			
			// Is this a group imageset?
			if(isset($res['group_gID'])){
				// This belongs to a group.
				$managedGroups = $userObj->getManagedGroups();
				
				
				$auth = false;
				foreach($managedGroups as $group){
					if($group[0] == $res['group_gID']) $auth = true;
				}
				
				if($auth == false) throw new PicBoardException("You are not allowed to delete this image. Please ask an administrator of the group.");
			}
			
			// Get a list of the images that are associated with this imageset
			
			$images = images::getListImages($isID);
			
			
			// Delete the images
			foreach($images as $image){
				images::deleteImage($userObj, $image['imgID']);
			}
			
			// Delete the imageset
			$sql = "DELETE FROM " . SYS_DB_DBNAME . ".imageset WHERE isID=" . $dbCon->real_escape_string((int)$isID);
			
			$dbCon->single($sql);
		
			$dbCon->commit();
			return true;
		} catch (Exception $e){
			$dbCon->rollback();
			return false;
		}

	}
	
}

?>